package com.webcash.CNBoard.Controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webcash.CNBoard.Model.Department;
import com.webcash.CNBoard.Model.Employee;
import com.webcash.CNBoard.Model.PositionName;
import com.webcash.CNBoard.Model.Team;
import com.webcash.CNBoard.Service.DepartmentService;
import com.webcash.CNBoard.Service.DepartmentTeamService;
import com.webcash.CNBoard.Service.EmployeeService;
import com.webcash.CNBoard.Service.PositionNameService;


@Controller
@SessionAttributes("login")
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	EmployeeService employeeService;
	
	@Autowired
	DepartmentService departmentService;
	
	@Autowired
	DepartmentTeamService departmentTeamService;
	
	@Autowired
	PositionNameService positionNameService;
	
	@RequestMapping
	public String adminAction(){
		return "admin/adminPage";
	}
	
	@RequestMapping("/addEmployee")
	public String addEmployeeAction(Model model, @ModelAttribute Team team){
		List<Department> deptlist  = departmentService.getDepartmentList();
		List<PositionName> pos = positionNameService.getPositionNameList();
		List<Team> teamlist = departmentTeamService.getTeamListbyDeptNo(1);
		model.addAttribute("teamlist", teamlist);
		model.addAttribute("pos", pos);
		model.addAttribute("deptList",  deptlist);
		return "admin/newEmployeeAddition";
	}
	
	@RequestMapping("/getTeamList")
	public String getTeamListAction(Model model, @ModelAttribute Team team){
			List<Team> teamlist = departmentTeamService.getTeamListbyDeptNo(Integer.valueOf(team.getDeptNo()));
			model.addAttribute("teamlist", teamlist);
		return "admin/teamlistPage";
	}
	
	@RequestMapping("/validateId")
	@ResponseBody
	public boolean validateIdcheck(@ModelAttribute Employee emp){
		Employee employee = employeeService.getEmployeeById(emp.getId());
		if(employee==null){
			return false;
		}else{
			return true;
		}
	}
	
	@RequestMapping("/addEmployeeAction")
	@ResponseBody
	public String addEmployeeAction(Model model, @ModelAttribute Employee employee){
		employeeService.insertEmployeebyEmp(employee);
		return "admin";
	}
	
	@RequestMapping("/addDepartment")
	public String addDepartmentPage(Model model){
		List<Employee> teamMember = employeeService.getAllEmployeeList();
		List<Employee> staff = new ArrayList<Employee>();				//사원
		List<Employee> supervisor = new ArrayList<Employee>();			//주임
		List<Employee> assistantManger= new ArrayList<Employee>();		//대리
		List<Employee> deputyManager = new ArrayList<Employee>();		//과장
		List<Employee> Manager = new ArrayList<Employee>();				//부장
		for(int i=0;i<teamMember.size();i++){
			switch(Integer.parseInt(teamMember.get(i).getPosition())){
			case 1:
				staff.add(teamMember.get(i));
				break;
			case 2:
				supervisor.add(teamMember.get(i));
				break;
			case 3:
				assistantManger.add(teamMember.get(i));
				break;
			case 4:
				deputyManager.add(teamMember.get(i));
				break;
			case 5:
				Manager.add(teamMember.get(i));
				break;
			}
			
		}
		model.addAttribute("staff", staff);
		model.addAttribute("supervisor", supervisor);
		model.addAttribute("assistantmanager", assistantManger);
		model.addAttribute("deputymanager", deputyManager);
		model.addAttribute("manager", Manager);
		return "admin/newDepartmentAddition";
	}
	
	@RequestMapping("/addDeptAction")
	@ResponseBody
	public String addDeptAction(Model model, @ModelAttribute Department department){
		departmentService.insertDepartmentbyDept(department);
		return "success";
	}
	
	
	
	
}
